/**
 * Created by Amaris on 2016/12/6.
 */


var util = require('../utils/util.js');
var daoUtil = require('../utils/daoUtil');
var addressbookClass = require('../class/addressbook.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var addressbook = new addressbookClass();

        var checkResult = addressbook.checkParams(
            [
                'company',
                'name',
                'phone',
                'createUid'
            ],
            params);
        params.enable = "1";
        if (checkResult) {
            cb(util.getErrorRes('400', checkResult), null);
            return;
        } else {
            addressbook.init(params);
        }

        models.addressbook.create(addressbook.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getInfo: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var addressbook = new addressbookClass();

        var checkResult = addressbook.checkParams(['rowId'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            addressbook.init(params);
        }

        models.addressbook.findOne(addressbook.getParams()).populate("company").populate("department").exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));

        });
    },
    //获取卖方联系人列表
    getAddressbookListByCompany: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }
        var addressbook = new addressbookClass();
        var checkResult = addressbook.checkParams(['company'], params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            addressbook.init(params);
        }

        models.addressbook.find(addressbook.getParams()).populate('company').populate("department").exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //获取联系人列表
    getList: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        models.addressbook.find().populate('company').populate("department").exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }
            cb(null, util.getSuccessRes(data));
        });
    },
    //查询联系人
    query: function (params, cb) {

        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

    },

    // update: function (params, cb) {
    //
    //     var models = util.models;
    //
    //     if (!models) {
    //         var err = new Error();
    //         err.status = 500;
    //         err.message = "数据库连接失败";
    //         cb(err, null);
    //         return;
    //     }
    //
    //     var addressbook = new addressbookClass();
    //     var checkResult = addressbook.checkParams(
    //         ['rowId',
    //             'name'
    //         ], params);
    //
    //     if (checkResult) {
    //         cb(util.getErrorRes('400', checkResult), null);
    //         return;
    //     } else {
    //         addressbook.init(params);
    //     }
    //
    //
    //     models.addressbook.update(params.rowId, addressbook.getParams()).exec(function (err, data) {
    //         if (err || !data) {
    //             console.log(err);
    //             var err = new Error();
    //             err.status = 500;
    //             err.message = '系统错误';
    //             cb(err, null);
    //             return;
    //         }
    //         if (data && data.length == 0) {
    //             var err = new Error();
    //             err.status = 401;
    //             err.message = '更新失败';
    //             cb(err, null);
    //             return;
    //         }
    //         cb(null, util.getSuccessRes(data));
    //     });
    // },
    update: function (params,cb) {
        var validate = daoUtil.validate(['rowId','name|enable'], params);
        if(!validate.valid){
            cb(validate.error);
            return;
        }
        var addressbook = new addressbookClass();
        addressbook.init(params);
        daoUtil.proxy(function(models){
            models.addressbook.update({rowId:params.rowId},params).exec(function (err, data) {
                if (err || !data) {
                    cb(util.getSysError());
                }else{
                    cb(null, util.getSuccessRes(data));
                }
            });
        },cb);
    },

};



